using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Floor : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("物体已落在地板上！");
        Goods gs = collision.gameObject.GetComponent<Goods>();
        if (gs.IsOnFloor) return;
        StartCoroutine(DestroyAfterDelay(collision.gameObject));
        GlobalEventManager.EmitEvent<int>("DropEvent", 1); //落地
        gs.SetOnFloor(true);
    }

    IEnumerator DestroyAfterDelay(GameObject obj)
    {
        yield return new WaitForSeconds(1f);
        Destroy(obj);
        int allCount = GoodsManager.Instance.GetAllGoods().Count;
        Tools.Log("剩余：" + allCount + "个");

        if (allCount <= 0 && !Data.IsGameOver)
        {
            Data.IsGameOver = true;
            CountdownManager.Instance.Stop(); // 停止倒计时
            PanelManager.Instance.OpenPanel<PassPanel>();

            Cart.Instance.ClearAllGoods();
        }
    }
}
